﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace MoneyBook.Controls {

    [TemplatePart(Name = GroupsHostPanelName, Type = typeof(Panel))]
    [TemplatePart(Name = ItemsHostPanelName, Type = typeof(Panel))]
    [TemplatePart(Name = ElementsContainerName, Type = typeof(HeaderedItemsControl))]
    public class GroupingViewGroup : Control {

        private const string GroupsHostPanelName = "GroupsHost";
        private const string ItemsHostPanelName = "ItemsHost";
        private const string ElementsContainerName = "ElementsContainer";
        private HeaderedItemsControl elementsContainer = null;
        private Panel groupsHost = null;
        private Panel itemsHost = null;
        private GroupingView owner = null;
        private CollectionViewGroup viewGroup = null;
        private object source = null;

        public GroupingViewGroup(GroupingView owner, CollectionViewGroup viewGroup) {
            //todo owner null check.
            this.DefaultStyleKey = typeof(GroupingViewGroup);
            this.owner = owner;
            this.viewGroup = viewGroup;
        }

        internal object Source {
            get { return source; }
            set {
                this.source = value;
                this.DataContext = this.source;
            }
        }

        public DataTemplate GroupTemplate {
            get { return (DataTemplate)GetValue(GroupTemplateProperty); }
            set { SetValue(GroupTemplateProperty, value); }
        }

        public static readonly DependencyProperty GroupTemplateProperty =
            DependencyProperty.Register("GroupTemplate", typeof(DataTemplate), typeof(GroupingViewGroup), new PropertyMetadata(OnGroupTemplateChanged));

        private static void OnGroupTemplateChanged(object sender, DependencyPropertyChangedEventArgs e) {

        }

        public override void OnApplyTemplate() {
            base.OnApplyTemplate();

            this.groupsHost = this.GetTemplateChild(GroupsHostPanelName) as Panel;
            this.itemsHost = this.GetTemplateChild(ItemsHostPanelName) as Panel;

            this.elementsContainer = this.GetTemplateChild(ElementsContainerName) as HeaderedItemsControl;
            AttachElements();
            this.owner.CheckLastSelectedItemSource();
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
            //TODO: 将来的には折りたためるように
            e.Handled = true;
        }
        private bool isDispalyGroups = false;

        private void AttachElements() {
            if (this.viewGroup == null) {
                return;
            }
            if (this.viewGroup.IsBottomLevel && this.itemsHost != null) {
                isDispalyGroups = false;
                foreach (object source in this.viewGroup.Items) {
                    GroupingViewItem item = owner.CreateItem(source, false);
                    this.itemsHost.Children.Add(item);
                }
            } else if (!this.viewGroup.IsBottomLevel && this.groupsHost != null) {
                isDispalyGroups = true;
                foreach (CollectionViewGroup groupView in this.viewGroup.Items) {
                    GroupingViewGroup group = owner.CreateGroup(groupView);
                    this.groupsHost.Children.Add(group);
                }
            }
            UpdateState();
        }

        private void UpdateState() {
            VisualStateManager.GoToState(this, isDispalyGroups ? "DisplayGroups" : "DisplayItems", false);
        }

    }
}
